package com.eet.excel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.eet.excel.model.OrderModel;
import com.eet.excel.service.ExportService;
import com.eet.export.pojo.ExportExcelDto;
import lombok.AllArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;

/**
 * 导出控制器
 *
 * @author Administrator
 */
@Controller
@AllArgsConstructor
@RequestMapping("export")
public class ExportController {
    private final ExportService exportService;
    private final com.eet.export.service.ExportService exportService2;

    /**
     * 基本导出
     *
     * @param response 响应
     */
    @GetMapping("base-export")
    public void baseExport(HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 防止文件名中文乱码
        String fileName = URLEncoder.encode("测试Excel", "utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        // 导出文件流
        EasyExcel.write(response.getOutputStream(), OrderModel.class).sheet("测试模板").doWrite(data());
    }

    /**
     * 模拟数据库获取数据
     *
     * @return 订单Excel
     */
    private List<OrderModel> data() {
        List<OrderModel> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            OrderModel orderModel = OrderModel.builder()
                    .id(UUID.randomUUID().toString())
                    .orderAmount(5 + i)
                    .flowNum(UUID.randomUUID().toString())
                    .description("测试商品" + i)
                    .price(BigDecimal.valueOf(75 + i))
                    .build();
            list.add(orderModel);
        }
        return list;
    }

    /**
     * 根据模板下载
     */
    @GetMapping("template")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        // 模板路径
        ClassPathResource resource = new ClassPathResource("template/excel-template.xlsx");
        String fileName = String.valueOf(System.currentTimeMillis());
        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream()).withTemplate(resource.getInputStream()).sheet().doFill(data());
    }

    /**
     * 动态导出Excel
     */
    @GetMapping("dynamic/excel")
    public void dynamicExport(HttpServletResponse response) throws IOException {
        Map<String, String> map = new LinkedHashMap<>(5);
        map.put("flowNum", "流水序列号");
        map.put("price", "价格");
        map.put("description", "描述信息");
        map.put("orderAmount", "订单数量");
        map.put("id", "主键ID");

        List<OrderModel> data = exportService.findAll();
        exportService2.dynamicExport(ExportExcelDto.builder()
                .fileName("李四")
                .sheetNames(Collections.singletonList("呵呵哒"))
                .headMap(map)
                .excelTypeEnum(ExcelTypeEnum.XLSX)
                .tableData(data)
                .build(), response);
    }
}
